package cn.openea.perpartner.meily.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.openea.perpartner.meily.util.UserContext;
import cn.openea.perpartner.meily.web.convert.SkuDataConvert;
import cn.openea.perpartner.meily.web.entity.ShopProductSku;
import cn.openea.perpartner.meily.web.entity.ShopProductSpec;
import cn.openea.perpartner.meily.web.entity.ShopProductSpecValue;
import cn.openea.perpartner.meily.web.entity.dto.SkuDataDto;
import cn.openea.perpartner.meily.web.mapper.ShopProductSkuMapper;
import cn.openea.perpartner.meily.web.mapper.ShopProductSpecMapper;
import cn.openea.perpartner.meily.web.mapper.ShopProductSpecValueMapper;
import cn.openea.perpartner.meily.web.service.IShopProductSkuService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商品sku表(ShopProductSku)表服务实现类
 *
 * @author Weird077
 * @since 2023-02-15 15:31:48
 */
@Service("shopProductSkuService")
public class ShopProductSkuServiceImpl extends ServiceImpl<ShopProductSkuMapper, ShopProductSku> implements IShopProductSkuService {

    @Autowired
    private ShopProductSpecMapper productSpecMapper;
    @Autowired
    private ShopProductSpecValueMapper productSpecValueMapper;
    @Autowired
    private SkuDataConvert skuDataConvert;

    @Override
    public JSONObject skuData(String productId) {
        LambdaQueryWrapper<ShopProductSku> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShopProductSku::getProductId, productId);
        wrapper.orderByAsc(ShopProductSku::getSort);
        wrapper.orderByAsc(ShopProductSku::getUpdateTime);
        List<ShopProductSku> skuList = list(wrapper);
        return createSkuData(skuList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public JSONObject skuCreate(JSONObject object) {
        SkuDataDto skuData = skuDataConvert.toSkuDataList(object);
        List<ShopProductSku> skuList = skuData.getSkuList();
        skuList.forEach(sku -> {
            sku.setCreateUser(UserContext.getUserId());
            sku.setUpdateUser(UserContext.getUserId());
        });
        saveOrUpdateBatch(skuList);
        List<ShopProductSpec> specList = skuData.getSpecList();
        specList.forEach(spec -> {
           LambdaQueryWrapper<ShopProductSpec> wrapper= Wrappers.<ShopProductSpec>lambdaQuery()
                    .eq(ShopProductSpec::getProductId, spec.getProductId())
                    .eq(ShopProductSpec::getSpecId, spec.getSpecId());
            ShopProductSpec productSpec= productSpecMapper.selectOne(wrapper);
            if (BeanUtil.isEmpty(productSpec)) {
                spec.setCreateUser(UserContext.getUserId());
                spec.setUpdateUser(UserContext.getUserId());
                productSpecMapper.insert(spec);
            }else {
                spec.setId(productSpec.getId());
                spec.setUpdateUser(UserContext.getUserId());
                productSpecMapper.updateById(spec);
            }
        });
        List<ShopProductSpecValue> specValueList = skuData.getSpecValueList();
        specValueList.forEach(specValue -> {
            LambdaQueryWrapper<ShopProductSpecValue> wrapper= Wrappers.<ShopProductSpecValue>lambdaQuery()
                    .eq(ShopProductSpecValue::getProductId, specValue.getProductId())
                    .eq(ShopProductSpecValue::getSpecId, specValue.getSpecId())
                    .eq(ShopProductSpecValue::getSpecValueId, specValue.getSpecValueId());
            ShopProductSpecValue productSpecValue= productSpecValueMapper.selectOne(wrapper);
            if (BeanUtil.isEmpty(productSpecValue)) {
                specValue.setCreateUser(UserContext.getUserId());
                specValue.setUpdateUser(UserContext.getUserId());
                productSpecValueMapper.insert(specValue);
            }else {
                specValue.setId(productSpecValue.getId());
                specValue.setUpdateUser(UserContext.getUserId());
                productSpecValueMapper.updateById(specValue);
            }
        });
        return createSkuData(skuList);
    }

    private JSONObject createSkuData(List<ShopProductSku> skuList) {
        JSONObject skuData = new JSONObject();
        skuList.stream().forEach(sku -> {
            skuData.put("skus[" + sku.getData() + "][id]", sku.getId());
            skuData.put("skus[" + sku.getData() + "][productId]", sku.getProductId());
            skuData.put("skus[" + sku.getData() + "][merchantId]", sku.getMerchantId());
            skuData.put("skus[" + sku.getData() + "][name]", sku.getName());
            skuData.put("skus[" + sku.getData() + "][picture]", sku.getPicture());
            skuData.put("skus[" + sku.getData() + "][price]", sku.getPrice());
            skuData.put("skus[" + sku.getData() + "][marketPrice]", sku.getMarketPrice());
            skuData.put("skus[" + sku.getData() + "][costPrice]", sku.getCostPrice());
            skuData.put("skus[" + sku.getData() + "][wholesalePrice]", sku.getWholesalePrice());
            skuData.put("skus[" + sku.getData() + "][stock]", sku.getStock());
            skuData.put("skus[" + sku.getData() + "][code]", sku.getCode());
            skuData.put("skus[" + sku.getData() + "][barcode]", sku.getBarcode());
            skuData.put("skus[" + sku.getData() + "][productWeight]", sku.getProductVolume());
            skuData.put("skus[" + sku.getData() + "][productVolume]", sku.getProductWeight());
            skuData.put("skus[" + sku.getData() + "][sort]", sku.getSort());
            skuData.put("skus[" + sku.getData() + "][status]", sku.getStatus());
            skuData.put("skus[" + sku.getData() + "][data]", sku.getData());
        });
        return skuData;
    }
}

